<template>
  <modal-container title="试算" v-el:modal v-ref:modal class="edit-modal">
    <form @submit.prevent @reset.prevent v-el:trialform>
      <div>
				<table class="table table-hover">
					<tbody>
						  <tr>
							 <td>交易渠道</td>
							 <td>
								<select name="chnl_no" class="form-control" v-model="modal.chnl_no">
									<option v-for="channel in channels" :value="channel.value">
										{{channel.key}}
									</option>
								</select>
							 </td>
						  </tr>
							<tr>
								<td>交易日期</td>
								<td>
                  <div class="input-append date form_datetime">
                			<input class=form-control type="text" name="date_time" v-model="modal.trading_date" readonly>
                			<span class="add-on"><i class="icon-th"></i></span>
                	</div>
								</td>
							</tr>
							<tr>
								<td>交易时间</td>
								<td>
                  <form-select-time :fld-value.sync="modal.trading_time"></form-select-time>
								</td>
							</tr>
							<tr>
								<td>交易金额</td>
								<td>
									<input type="text" v-model="modal.trading_m" class="form-control" name="trading_m">
								</td>
							</tr>
					</tbody>
				</table>
			</div>
      <div class="modal-footer">
        <v-button data-dismiss="modal">关闭</v-button>
        <v-button theme="primary" type="submit">保存</v-button>
      </div>
    </form>

  </modal-container>
</template>

<script>
import FormSelectTime from './time'
import serialize from 'form-serialize'
import {dateTime} from 'ui/collections/general.js'
export default {
  components: {
    FormSelectTime
  },
  data: () => ({
    modal: {
      actId: '',
      mchtId: '',
      chnl_no: 'wechat',
      trading_time: ['00', '00', '00']
    },
    channels: [
      {key: '微信支付',value: 'wechat'},
      // {key: '收付直通车',value: 'paytrain'},
      {key: '银联',value: 'union'},
      {key: '支付宝',value: 'alipay'},
      {key: '线下',value: 'line'}
    ]
  }),
  methods: {
    show() {
      this.$refs.modal.init()
    },
    submit(data) {
      this.$post({
        url: "/api/v2" + projectName_Url + "/plat/act/trial/" + `${this.modal.actId}`,
        method: 'POST',
        data
      }).then(async res => {
        if (res.respCode == '00') {
          let result = res.content.result
          let message
          if (result[0].couponName) {
            message = result[0].couponName
          } else {
            message = '没有优惠'
          }
          await Midway.confirm({
            title: "试算成功",
            content: message
          })
          this.$refs.modal.hide()
          this.$emit('reset-table')
        } else {
          await Midway.confirm({
            title: "提示",
            content: res.errorInfo
          })
        }
      })
    }
  },
  ready() {
    dateTime({
      format: "yyyy-mm-dd",
      minView: "2",
      startDate: "",
      endDate: ""
    })
    let self = this;
    $(self.$els.trialform).validate({
      submitHandler() {
        var form = self.$els.trialform;
        var _data = serialize(form, {
          hash: true,
          disabled: true,
          empty: true
        });
        self.submit({
          mchtId: self.modal.mchtId,
          trialStr: `${self.modal.mchtId}|${(_data.date_time).split('-').join('')}|${self.modal.trading_time.join('')}|0|0|${_data.trading_m}|${_data.chnl_no}|0|0|0|0|0|0|0|0|0|0|0|0|0`
        })
      },
      rules: {
        date_time: {required: true},
        trading_m: {
          required: true,
          int: true,
          intMoney: true
        },
      }
    })
  }
}
</script>

<style lang="css" scoped>
</style>
